/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package uk.co.senab.photoview.sample;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

public class PhotoViewPagerActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    TextView tv_index;

//    String [] imgs = {"http://img2.ph.126.net/GFtUbpoqAzBGeZWNP7qEtQ==/6619306289816528033.jpg",
//            "http://imgsrc.baidu.com/forum/w%3D580/sign=878d15381e950a7b75354ecc3ad0625c/0c6982cd7b899e51c436741a41a7d933c9950d93.jpg",
//            "http://img0.ph.126.net/w-l4YvZ4LpKLw1cLJcOh8Q==/6630310202187362718.jpg",
//            "http://ww2.sinaimg.cn/large/693d698bgw1eqq2yp948pj20tm18g47a.jpg",
//            "http://ww2.sinaimg.cn/large/005Auy4Djw1eqlq4tb5pmj318g0tmwss.jpg",
//            "http://ww1.sinaimg.cn/large/005Auy4Djw1ev1ac7yixtj32233347wi.jpg",
//            "http://ww2.sinaimg.cn/large/005Auy4Djw1epn1g9llsbj30tm18gqhj.jpg",
//            "http://img2.ph.126.net/Ed2fr6awRVIz26cCjHen6w==/1798906576258159766.jpg",
//            "http://img1.ph.126.net/c2aQsaHwRlEFOTJOU131Kw==/6599314969401853297.jpg",
//            "http://s15.sinaimg.cn/mw690/005Auy4Dgy6KXJ7NiKWae&690",
//            "http://img2.imgtn.bdimg.com/it/u=1741283743,65784715&fm=21&gp=0.jpg",
//            "http://img5.imgtn.bdimg.com/it/u=3362344683,4033947312&fm=21&gp=0.jpg",
//            "http://img1.imgtn.bdimg.com/it/u=2918746153,1394770288&fm=21&gp=0.jpg",
//            "http://img1.imgtn.bdimg.com/it/u=656882440,524467509&fm=21&gp=0.jpg",
//            "http://img2.ph.126.net/5slhQ5inzFT03p7LKWXM5A==/6608604743143743200.jpg",
//            "http://img2.ph.126.net/MinYa72NW71s0zaI43qoGg==/6608608041678217433.jpg",
//            "http://img1.imgtn.bdimg.com/it/u=1922902409,611435282&fm=21&gp=0.jpg",
//            "http://img1.ph.126.net/Ox_-48XlCBdfU9fg5CJdow==/4842776974407263566.jpg",
//            "http://img0.ph.126.net/AtiUR7lRMIGSdytRmRO4YA==/6608738883562254042.jpg",
//            "http://img3.imgtn.bdimg.com/it/u=3460886037,2333310496&fm=21&gp=0.jpg",};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);

        tv_index = (TextView) findViewById(R.id.tv_index);
        ArrayList<String> list = null;
        int index = 0;
        if (getIntent() != null){
            list = getIntent().getStringArrayListExtra("images");
            index = getIntent().getIntExtra("index", 0);
        }
//		index = 5;
//		list=new ArrayList<String>();
//		for(int i=0,size = imgs.length; i <size; i++){
//			list.add(imgs[i]);
//		}
        mViewPager.setAdapter(new SamplePagerAdapter(PhotoViewPagerActivity.this, list));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mViewPager.getAdapter().getCount() > 0){
                    tv_index.setText((position + 1) +"/"+ mViewPager.getAdapter().getCount());
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mViewPager.setCurrentItem(index);
        index++;
        if (mViewPager.getAdapter().getCount() > 0){
            tv_index.setText(index+"/"+ mViewPager.getAdapter().getCount());
        }
    }

    class SamplePagerAdapter extends PagerAdapter {
        ArrayList<String> list;
        public SamplePagerAdapter(AppCompatActivity activity,  ArrayList<String> list) {
            this.activity = activity;
            this.list = list;
        }

        AppCompatActivity activity;

        @Override
        public int getCount() {
            return list == null ? 0 :list .size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
//			photoView.setImageResource(sDrawables[position]);


            Glide.with(activity).load(list.get(position)).into(photoView);
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
